package com.jservice.framework.utils;

import com.jfinal.log.Log;
import com.tencent.xinge.ClickAction;
import com.tencent.xinge.Message;
import com.tencent.xinge.MessageIOS;
import com.tencent.xinge.XingeApp;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * XingeUtils - 腾讯信鸽推送
 *
 * @Author cwledit
 * @DATE 2017/7/5.
 */
public class XingeUtils {

    private static final Log log = Log.getLog(XingeUtils.class);

    private static final long ANDROID_ACCESSID = 2100262292l;
    private static final String ANDROID_SECRET = "8e912fb31107b365d92d3df56aced463";

    private static final long IOS_ACCESSID = 2200262269l;
    private static final String IOS_SECRET = "41aec3ef73efe3d838ed17bb40e51988";

    /**
     * IOS端消息推送
     * @param content   消息内容
     * @param account   发送消息目标账户
     * @param badge     角标
     * @param id        消息ID
     */
    public static void iosPushNotice(String content, String account,int badge, String id){
        MessageIOS message = new MessageIOS();
        message.setAlert(content);
        message.setBadge(badge);
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("id", id);
        message.setCustom(custom);
        XingeApp xingeIOS = new XingeApp(IOS_ACCESSID, IOS_SECRET);
        JSONObject ret = xingeIOS.pushSingleAccount(0, account, message, XingeApp.IOSENV_PROD);
        log.info("IOS 推送消息:["+account+"] - "+ret);
    }

    /**
     * 安卓端消息推送
     * @param content
     * @param account
     * @param badge
     * @param id
     */
    public static void androidPushNotice(String content, String account,int badge, String id){
        Message message = new Message();
        ClickAction action = new ClickAction();
        action.setActionType(ClickAction.TYPE_ACTIVITY);
        action.setActivity("com.wzsmk.citizencardapp.MessageDetailActivity_");
        message.setAction(action);
        message.setType(Message.TYPE_NOTIFICATION);
        message.setTitle("消息通知");
        message.setContent(content);
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("id", id);
        custom.put("badge", badge);
        message.setCustom(custom);
        XingeApp xingeAndroid = new XingeApp(ANDROID_ACCESSID, ANDROID_SECRET);
        JSONObject ret = xingeAndroid.pushSingleAccount(0,account,message);
        log.info("ANDROID 推送消息:["+account+"] - "+ret);
    }



    public static void main(String[] args) {
//        long accessId = 2100262292l;
        XingeApp xinge = new XingeApp(ANDROID_ACCESSID, ANDROID_SECRET);
        String account = "330327199303060953";
        JSONObject ret2 = xinge.queryTokensOfAccount(account);
        System.out.println(ret2);
//
        Message message = new Message();
        ClickAction action = new ClickAction();
        action.setActionType(ClickAction.TYPE_ACTIVITY);
        action.setActivity("com.wzsmk.citizencardapp.MessageDetailActivity_");
        message.setAction(action);
        message.setType(Message.TYPE_NOTIFICATION);
        message.setTitle("消息通知");
        message.setContent("您尾号1029的市民卡账户2016-12-10日16:48:30电子钱包充值撤销-0.01元，余额以卡面为准。详询96225");

        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("id", "1");
        custom.put("badge", "4");
        message.setCustom(custom);
        JSONObject ret = xinge.pushSingleAccount(0,account,message);

//        JSONObject ret = xinge.pushSingleAccount(0, "330522199211153916", message, XingeApp.IOSENV_DEV);

//        JSONObject ret = XingeApp.pushAccountIos(IOS_ACCESSID, IOS_SECRET, "测试222222222", "330327199303060953", XingeApp.IOSENV_PROD);
        System.out.println(ret);
//        System.out.println(XingeApp.pushAccountAndroid(000, "secretKey", "test", "测试", "account"));

    }

}
